using EasyTest.Application.Service.QuestionBank.Enum;
using System.Text.Json;

namespace EasyTest.Application.Service.QuestionBank.Helper;

/// <summary>
/// 题目数据验证帮助类
/// </summary>
public static class QuestionValidateHelper
{
    /// <summary>
    /// 验证选项格式
    /// </summary>
    public static void ValidateOptions(string options, QuestionTypeEnum questionType)
    {
        if (string.IsNullOrEmpty(options))
        {
            if (questionType is QuestionTypeEnum.SingleChoice or QuestionTypeEnum.MultipleChoice)
                throw Oops.Oh("选择题必须包含选项");
            return;
        }

        try
        {
            var optionList = JsonSerializer.Deserialize<List<QuestionOption>>(options);
            if (optionList == null || !optionList.Any())
                throw Oops.Oh("选项格式错误");

            // 检查选项内容
            foreach (var opt in optionList)
            {
                if (string.IsNullOrEmpty(opt.Label) || string.IsNullOrEmpty(opt.Value))
                    throw Oops.Oh("选项必须包含标签和值");
            }
        }
        catch
        {
            throw Oops.Oh("选项必须是有效的JSON格式");
        }
    }

    /// <summary>
    /// 验证答案格式
    /// </summary>
    public static void ValidateAnswer(string answer, QuestionTypeEnum questionType, string options)
    {
        if (string.IsNullOrEmpty(answer))
            throw Oops.Oh("答案不能为空");

        switch (questionType)
        {
            case QuestionTypeEnum.TrueFalse:
                if (answer != "0" && answer != "1")
                    throw Oops.Oh("判断题答案必须是0或1");
                break;

            case QuestionTypeEnum.SingleChoice:
                ValidateChoiceAnswer(answer, options, false);
                break;

            case QuestionTypeEnum.MultipleChoice:
                ValidateChoiceAnswer(answer, options, true);
                break;

            case QuestionTypeEnum.FillInBlank:
                ValidateFillBlankAnswer(answer);
                break;
        }
    }

    /// <summary>
    /// 验证选择题答案
    /// </summary>
    private static void ValidateChoiceAnswer(string answer, string options, bool isMultiple)
    {
        try
        {
            var optionList = JsonSerializer.Deserialize<List<QuestionOption>>(options);
            var answerList = JsonSerializer.Deserialize<List<string>>(answer);

            if (answerList == null || !answerList.Any())
                throw Oops.Oh("答案格式错误");

            if (!isMultiple && answerList.Count > 1)
                throw Oops.Oh("单选题只能选择一个答案");

            // 验证答案是否在选项中存在
            var optionValues = optionList.Select(x => x.Value).ToList();
            foreach (var ans in answerList)
            {
                if (!optionValues.Contains(ans))
                    throw Oops.Oh($"答案 {ans} 不在选项中");
            }
        }
        catch
        {
            throw Oops.Oh("答案必须是有效的JSON格式");
        }
    }

    /// <summary>
    /// 验证填空题答案
    /// </summary>
    private static void ValidateFillBlankAnswer(string answer)
    {
        try
        {
            var answerList = JsonSerializer.Deserialize<List<string>>(answer);
            if (answerList == null || !answerList.Any())
                throw Oops.Oh("填空题答案格式错误");
        }
        catch
        {
            throw Oops.Oh("填空题答案必须是有效的JSON格式");
        }
    }

    /// <summary>
    /// 验证标签格式
    /// </summary>
    public static void ValidateTags(string tags)
    {
        if (string.IsNullOrEmpty(tags)) return;

        try
        {
            var tagList = JsonSerializer.Deserialize<List<string>>(tags);
            if (tagList == null)
                throw Oops.Oh("标签格式错误");
        }
        catch
        {
            throw Oops.Oh("标签必须是有效的JSON格式");
        }
    }

    /// <summary>
    /// 验证附件格式
    /// </summary>
    public static void ValidateAttachments(string attachments)
    {
        if (string.IsNullOrEmpty(attachments)) return;

        try
        {
            var attachList = JsonSerializer.Deserialize<List<FileInfo>>(attachments);
            if (attachList == null)
                throw Oops.Oh("附件格式错误");

            foreach (var attach in attachList)
            {
                if (string.IsNullOrEmpty(attach.Url))
                    throw Oops.Oh("附件URL不能为空");
            }
        }
        catch
        {
            throw Oops.Oh("附件必须是有效的JSON格式");
        }
    }
}

/// <summary>
/// 选项模型
/// </summary>
public class QuestionOption
{
    public string Label { get; set; }
    public string Value { get; set; }
}

/// <summary>
/// 附件信息
/// </summary>
public class FileInfo
{
    public string Name { get; set; }
    public string Url { get; set; }
    public long Size { get; set; }
} 